﻿using System;
using System.Xml;

namespace Graphe.Collations
{
    public class Sacrum : ICollationElement, IComparable<Sacrum>
    {
        #region Constructor

        /// <summary>
        /// Default constructor.
        /// </summary>
        public Sacrum()
        {
            Manuscripts = new ManuscriptCollection();
        }

        /// <summary>
        /// Constructor for fully initializing the sacrum.
        /// </summary>
        /// <param name="text">The text of the word abbreviated.</param>
        /// <param name="representation">The abbreviated form of the word.</param>
        /// <param name="manuscripts">The manuscripts which have the sacrum.</param>
        public Sacrum(string text, string representation, ManuscriptCollection manuscripts)
            : base()
        {
            Text = text;
            Representation = representation;
            Manuscripts = manuscripts;
        }

        #endregion
        #region Public Properties

        /// <summary>
        /// The text of the word abbreviated.
        /// </summary>
        public string Text { get; set; }

        /// <summary>
        /// The abbreviated form of the word.
        /// </summary>
        public string Representation { get; set; }

        /// <summary>
        /// The manuscripts which have the sacrum.
        /// </summary>
        public ManuscriptCollection Manuscripts { get; set; }

        #endregion
        #region ICollationElement Members

        public void Parse(XmlReader reader, CollationDebugMessages debugMessages)
        {
            reader.ReadToFollowing("Text");
            reader.Read();
            Text = reader.ReadContentAsString().Trim();

            reader.ReadToFollowing("Representation");
            reader.Read();
            Representation = reader.ReadContentAsString().Trim();

            reader.ReadToFollowing("Manuscripts");
            Manuscripts = new ManuscriptCollection();
            Manuscripts.Parse(reader, debugMessages);
        }

        public void Render(XmlTextWriter writer)
        {
            writer.WriteStartElement("Sacrum");
            {
                writer.WriteStartElement("Text");
                writer.WriteString(Text);
                writer.WriteEndElement();

                writer.WriteStartElement("Representation");
                writer.WriteString(Representation);
                writer.WriteEndElement();

                writer.WriteStartElement("Manuscripts");
                foreach (Manuscript manuscript in Manuscripts)
                    manuscript.Render(writer);
                writer.WriteEndElement();
            }
            writer.WriteEndElement();
        }

        #endregion
        #region IComparable<Sacrum> Members

        public int CompareTo(Sacrum other)
        {
            if (Text != other.Text || Representation != other.Representation)
                return -1;

            return Manuscripts.CompareTo(other.Manuscripts);
        }

        #endregion
    }
}
